package com.sjp.controller.admin;

import com.sjp.pojo.Comment;
import com.sjp.service.CommentService;
import com.sjp.util.Result;
import com.sjp.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/comment")
    public String comment(Model model) {
        // 查询所有评论
        List<Comment> allComments = commentService.queryAllComments();
        model.addAttribute("allComments",allComments);
        return "admin/comment";
    }

    @RequestMapping("/comment/deleteComments")
    @ResponseBody
    public Result deleteComments(@RequestParam("ids") Integer[] ids) {
        int column = commentService.deleteComments(ids);
        if (column != ids.length) return ResultGenerator.genFailResult("删除失败");
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/reply")
    @ResponseBody
    public Result reply(@RequestParam("id") Integer id, @RequestParam("replyBody") String replyBody) {
        if (commentService.reply(id, replyBody)) return ResultGenerator.genSuccessResult();
        return ResultGenerator.genFailResult("回复失败");
    }

}
